package com.six.elemo_manger.ui;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.avos.avoscloud.AVCloudQueryResult;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.CloudQueryCallback;
import com.avos.avoscloud.FindCallback;
import com.six.elemo_manger.R;
import com.six.elemo_manger.adapter.OnlineAdapter;
import com.six.elemo_manger.adapter.TestAdapter;
import com.six.elemo_manger.bean.FoodList;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Fra_online extends Fragment {
    private android.support.v4.widget.SwipeRefreshLayout refreshLayout;
    private ListView listView;
    private View view;
    private List<AVObject> mylist =new ArrayList();
    private TestAdapter adapter;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
         view=inflater.inflate(R.layout.layout_online,container,false);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        listView=view.findViewById(R.id.lv_online);
        refreshLayout=view.findViewById(R.id.refresh);
        refreshLayout.setRefreshing(true);
        get();
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                showDel(mylist.get(position).getObjectId()+"");
                return false;
            }
        });
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                get();
            }
        });

    }
    /*
    获取数据
     */
    void get(){

        AVQuery<AVObject> avQuery = new AVQuery<>("FoodList");
        avQuery.orderByDescending("createdAt");
        avQuery.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if (e == null) {
                     adapter = new TestAdapter(list,getContext());
                    listView.setAdapter(adapter);
                    refreshLayout.setRefreshing(false);
                    adapter.notifyDataSetChanged();
                    mylist=list;
                    } else {
                    e.printStackTrace();
                }
            }
        });
    }

    private void showDel(final String objId){
        //    通过AlertDialog.Builder这个类来实例化我们的一个AlertDialog的对象
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        //    设置Title的内容
        builder.setTitle("警告");
        //    设置Content来显示一个信息
        builder.setMessage("确定删除吗？");
        //    设置一个PositiveButton
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                // 执行 CQL 语句实现删除一个 Todo 对象
                AVQuery.doCloudQueryInBackground("delete from FoodList where objectId='"+objId+"'", new CloudQueryCallback<AVCloudQueryResult>() {
                    @Override
                    public void done(AVCloudQueryResult avCloudQueryResult, AVException e) {
                        if(e==null ){
                            toast("删除成功");
                            get();

                        }
                        else{
                            toast("删除失败");
                            Log.d("del",e.toString());
                        }
                    }
                });

            }
        });
        //    设置一个NegativeButton
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {

            }
        });
        //    设置一个NeutralButton
        builder.setNeutralButton("忽略", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {

            }
        });
        //    显示出该对话框
        builder.show();


    }
    public void toast(String msg) {
        Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
    }

}
